﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Omu.AwesomeMvc;
namespace TCM.Controllers
{
    [Authorize]
    public class StudentController : Controller
    {
        private TCMContext db = new TCMContext();

        //
        // GET: /Student/

        [Authorize]
        public ActionResult Index()
        {
            return View(db.Students.ToList());
        }

        //
        // GET: /Student/Details/5

        public ActionResult Details(Guid? id = null)
        {
            Student Student = db.Students.Find(id);
            if (Student == null)
            {
                return HttpNotFound();
            }
            return View(Student);
        }

        //
        // GET: /Student/Create

        public ActionResult Create()
        {
            ViewBag.StatusID = new SelectList(db.RecordStates
                .ToList().AsEnumerable(), "StatusID", "StatusName");
            return View();
        }

        //
        // POST: /Student/Create

        [HttpPost]
        public ActionResult Create(Student Student)
        {
            if (ModelState.IsValid)
            {
                Student.StudentID  = Guid.NewGuid();
               // Student.StatusID = 1;
                db.Students.Add(Student);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StatusID = new SelectList(db.RecordStates.ToList().AsEnumerable(), "StatusID", "StatusName");
            return View(Student);
        }

        //
        // GET: /Student/Edit/5

        public ActionResult Edit(Guid? id = null)
        {
            Student Student = db.Students.Find(id);
            if (Student == null)
            {
                return HttpNotFound();
            }
            ViewBag.StatusID = new SelectList(db.RecordStates.ToList().AsEnumerable(), "StatusID", "StatusName", Student.StudentID);
            return View(Student);
        }
        
        //
        // POST: /Student/Edit/5

        [HttpPost]
        public ActionResult Edit(Student Student)
        {
            if (ModelState.IsValid)
            {
                db.Entry(Student).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StatusID = new SelectList(db.RecordStates.ToList().AsEnumerable(), "StatusID", "StatusName", Student.StudentID);
            return View(Student);
        }

        //
        // GET: /Student/Delete/5

        public ActionResult Delete(Guid? id = null)
        {
            Student Student = db.Students.Find(id);
            if (Student == null)
            {
                return HttpNotFound();
            }
            return View(Student);
        }

        //
        // POST: /Student/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            Student Student = db.Students.Find(id);
            db.Students.Remove(Student);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }

//    public class StudentLookupController : Controller
//    {
//        private TCMContext Db = new TCMContext();
//        public ActionResult GetItem(Guid? v)
//        {
//            var o = Db.Students.SingleOrDefault(f => f.StudentID == v) ?? new Student();

//            return Json(new KeyContent(o.StudentID, o.Name));
//        }

//        public ActionResult Search(string search, int page)
//        {
//            search = (search ?? "").ToLower().Trim();
//            var list = Db.Students.Where(f => f.Name.ToLower().Contains(search));
//            return Json(new AjaxListResult
//            {
//                Items = list.Skip((page - 1) * 7).Take(7).Select(o => new KeyContent(o.StudentID, o.Name)),
//                More = list.Count() > page * 7
//            });
//        }
//    }
}